package com.varian.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.varian.log.annotation.ApiLog;
import com.varian.service.annotation.Resubmit;
import com.varian.service.web.BaseController;
import com.varian.system.dto.SysRoleDTO;
import com.varian.system.dto.SysRoleGrantDTO;
import com.varian.system.dto.SysRoleListDTO;
import com.varian.system.dto.SysRolePageDTO;
import com.varian.system.service.ISysRoleService;
import com.varian.system.vo.SysRoleGrantVO;
import com.varian.system.vo.SysRoleVO;
import com.varian.tool.result.IR;
import com.varian.tool.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ben
 * @since 2024/7/2
 */
@Resubmit
@RestController
@Tag(name = "角色控制层")
@RequestMapping("/role")
public class SysRoleController extends BaseController<ISysRoleService> {

    @ApiLog
    @PostMapping
    @Operation(summary = "新增角色")
    @PreAuthorize("hasAuthority('role:add')")
    public IR add(@Validated @RequestBody SysRoleDTO dto){
        return R.status(baseService.add(dto));
    }

    @ApiLog
    @PutMapping("{id:\\d+}")
    @Operation(summary = "修改角色")
    @PreAuthorize("hasAuthority('role:modify')")
    public IR modify(@PathVariable("id") Long id, @Validated @RequestBody SysRoleDTO dto){
        return R.status(baseService.modify(id, dto));
    }

    @ApiLog
    @GetMapping("{id:\\d+}")
    @Operation(summary = "角色详情")
    @PreAuthorize("hasAuthority('role:detail')")
    public R<SysRoleVO> detail(@PathVariable("id") Long id){
        return R.data(baseService.detail(id));
    }

    @ApiLog
    @DeleteMapping("{id:\\d+}")
    @Operation(summary = "删除角色")
    @PreAuthorize("hasAuthority('role:delete')")
    public IR delete(@PathVariable("id") Long id){
        return R.status(baseService.delete(id));
    }

    @ApiLog
    @GetMapping("page")
    @Operation(summary = "角色分页列表")
    @PreAuthorize("hasAuthority('role:page')")
    public R<Page<SysRoleVO>> page(@Validated SysRolePageDTO dto){
        return R.data(baseService.page(dto));
    }

    @ApiLog
    @GetMapping("list")
    @Operation(summary = "角色列表")
    @PreAuthorize("hasAuthority('role:list')")
    public R<List<SysRoleVO>> list(@Validated SysRoleListDTO dto){
        return R.data(baseService.list(dto));
    }

    @ApiLog
    @GetMapping("/grant/{id:\\d+}")
    @Operation(summary = "角色权限数据")
    @PreAuthorize("hasAuthority('role:granted-list')")
    public R<SysRoleGrantVO> grantedList(@PathVariable("id") Long id){
        return R.data(baseService.grantedList(id));
    }

    @ApiLog
    @PutMapping("/grant/{id:\\d+}")
    @Operation(summary = "修改角色权限数据")
    @PreAuthorize("hasAuthority('role:grant')")
    public IR grant(@PathVariable("id") Long id, @RequestBody SysRoleGrantDTO dto){
        return R.status(baseService.grant(id, dto));
    }
}
